﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using GameStudio.Common;
using GameStudio.IDE.Common.Events;
using Microsoft.Practices.ServiceLocation;
using Utils.EventAggregator;
using GameStudio.IDE.Common;
using GameStudio.CreateProject;
using GameStudio.CodeGeneration;
using System.CodeDom;

namespace GameStudio.IDE.Controls
{
    [TemplatePart(Name = "btnOk", Type = typeof(ButtonBase))]
    [TemplatePart(Name = "btnCancel", Type = typeof(ButtonBase))]
    [TemplatePart(Name = "btnBrowse", Type = typeof(ButtonBase))]
    [TemplatePart(Name = "txtProjectName", Type = typeof(TextBoxBase))]
    [TemplatePart(Name = "txtFolderPath", Type = typeof(TextBoxBase))]
    [TemplatePart(Name = "cmbxProjectType", Type = typeof(ComboBox))]
    public class NewProjectControl : Control
    {
        ComboBox cmbxProjectType;
        ButtonBase btnOk;
        ButtonBase btnCancel;
        ButtonBase btnBrowse;
        TextBox txtProjectName;
        TextBox txtFolderPath;

       // public ProjectCreateInfo CurrentProject;
        //public ProjectFactory NewProjectFactory;

        public NewProjectControl()
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<NewClassLibraryEvent>().Subscribe(NewClassLibrary);
        }

        private void NewClassLibrary(string str)
        {
            txtProjectName.Text = "classLibraryProject1";
            cmbxProjectType.SelectedItem = "Class Project";
        }

        public void SetControl(string name, string type)
        {
            txtProjectName.Text = name;
            cmbxProjectType.SelectedItem = type;
        }

        //public NewProjectControl(GameStudio.Common.ProjectFactory passingProjectProperities)
        //{
        //    NewProjectFactory = passingProjectProperities;
        //}
        //WrapPanel lstProjectTypes;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            btnOk = (ButtonBase)GetTemplateChild("btnOk");
            btnOk.Click += new RoutedEventHandler(btnOk_Click);

            btnCancel = (ButtonBase)GetTemplateChild("btnCancel");
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);

            btnBrowse = (ButtonBase)GetTemplateChild("btnBrowse");
            btnBrowse.Click += new RoutedEventHandler(btnBrowse_Click);

            txtProjectName = (TextBox)GetTemplateChild("txtProjectName");
            txtFolderPath = (TextBox)GetTemplateChild("txtFolderPath");
            txtFolderPath.Text = ProjectCreateInfo.DefaultProjectLocation;

            cmbxProjectType = (ComboBox)GetTemplateChild("cmbxProjectType");

            //cmbxProjectType.DataContext = ProjectCreateInfo.TypesofProject;
            cmbxProjectType.Items.Add("Empty Project");
            cmbxProjectType.Items.Add("Class Library");
            cmbxProjectType.SelectedItem = cmbxProjectType.Items[0];
            //DataBind();//if i used bind
        }

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            GameStudio.IDE.Common.Project currentProject = new GameStudio.IDE.Common.Project();// Generate new project instance
            ProjectName = txtProjectName.Text;
            Location = txtFolderPath.Text;

            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();

            currentProject.Path = System.IO.Path.Combine(Location,ProjectName);
            currentProject.Name = ProjectName;
            // add it to the session as the current project opened
            Session.CurrentProject = currentProject;

            ProjectCreateInfo CreateProject = new ProjectCreateInfo(currentProject.Path, currentProject.Name, cmbxProjectType.SelectedItem.ToString());
            CreateProject.CreateProject();

            eventAggregator.GetEvent<CreateProjectEvent>().Publish(currentProject);

            GenerateClassInheritProgram();

            Window.GetWindow(btnOk).Close();

        }

        private void GenerateClassInheritProgram()
        {
            //GenerateEmptyClass emptyClass = new GenerateEmptyClass();
            //emptyClass.Generate();
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(btnCancel).Close();
        }

        void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            WinForms dlg = new WinForms();
            string SelectedPath = dlg.Browse_dlg();
            if (SelectedPath != null)
            {
                txtFolderPath.Text = SelectedPath; //fDialog.SelectedPath;
            }
        }

        public string ProjectName { get; set; }

        public string Location { get; set; }
    }
}
